package com.steellee.leeexcel;

import com.steellee.leeexcel.abstruct.PaOwnCheckExtend;
import com.steellee.leeexcel.dto.MetaSheetInfo;
import com.steellee.leeexcel.dto.PaExampleOneDto;
import com.steellee.leeexcel.dto.PaExampleTwoDto;
import com.steellee.leeexcel.util.CellStyleUtil;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OwnObjCheck extends PaOwnCheckExtend {
    @Override
    @SneakyThrows
    public Map<String, String> ownCheckStart(String errMsg, String errIndex, Map<String, Object> dataLists, List<MetaSheetInfo> metaSheet) {
        Map<String, String> result = new HashMap<>();
        StringBuffer msg = new StringBuffer(errMsg);
        StringBuffer index = new StringBuffer(errIndex);

        // sheet0
        List<Object> dataList = (List<Object>) dataLists.get("PaExampleOneDto");
        for (int i = 0; i < dataList.size(); i++) {
            PaExampleOneDto dto = (PaExampleOneDto) dataList.get(i);
            if (!"二级制度".equals(dto.getDocLevel())) {
                addMsg(msg, index, "docLevel", metaSheet.get(0), PaExampleOneDto.class, i);
            }
        }

        // sheet1
        dataList = (List<Object>) dataLists.get("PaExampleTwoDto");
        for (int i = 0; i < dataList.size(); i++) {
            PaExampleTwoDto dto = (PaExampleTwoDto) dataList.get(i);
            if (!"二级制度".equals(dto.getDocLevel1())) {
                addMsg(msg, index, "docLeve11", metaSheet.get(1), PaExampleTwoDto.class, i);
            }
        }

        if (!StringUtils.isEmpty(msg.toString()) && !StringUtils.isEmpty(index.toString())) {
            result.put("errMsg", msg.toString());
            result.put("errIndex", index.toString());
        }
        return result;
    }

    private void addMsg(StringBuffer errMsg, StringBuffer errIndex, String fieldName, MetaSheetInfo metaSheet, Class clazz, int num) throws Exception {
        String errInfo = "sheet:\"" + metaSheet.getSheetName() +
                "\"第" + (num + 2) + "行:" + CellStyleUtil.getCellName(clazz, fieldName);
        if (!StringUtils.isEmpty(errMsg.toString())) {
            errMsg.append(",");
        }
        errMsg.append(errInfo);
        if (!StringUtils.isEmpty(errIndex.toString())) {
            errIndex.append(";");
        }
        errIndex.append(metaSheet.getSheetNo() + "," + (num + 1) + CellStyleUtil.getCellColumn(clazz, fieldName));
    }
}